/*
 * XMLReader.java
 *
 * Created on April 27, 2007, 4:58 PM
 *
 *  Life Logger v1.0 
    Copyright (C) 2007  Anand Kishore (anand@semanticvoid.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package com.semanticvoid.lifelogger.xml.parser;

import com.semanticvoid.lifelogger.configuration.ConfigManager;
import com.semanticvoid.lifelogger.configuration.Constants;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 * Super class for all XMLReaders
 * @author anand kishore
 */
public class XMLReader {
    
    
    /** The path of the local xml file */
    protected String path = null;
    
    /**
     * The xml document object
     */
    protected Vector<String> xmlFilePaths = null;
    
    /**
     * The config manager instance
     */
    ConfigManager mgr = ConfigManager.getInstance();
    
    /**
     * Creates a new instance of XMLReader
     * @param path the path of the xml file
     */
    public XMLReader(String path) {
        this.path = path;
        xmlFilePaths = new Vector<String>();
    }
    
    /**
     * Form documents from all the files in the directory
     */
    public boolean formDocuments() {        
        // If director iterate through all the files
        if(new File(path).isDirectory()) {
            String[] files = new File(path).list();
            
            for(String file : files) {
                xmlFilePaths.add(path + mgr.getValueForKey(Constants.KEY_FOR_FILE_SEPARATOR) + file);
            }
        } else {
            xmlFilePaths.add(path);
        }
        
        return true;
    }
    
    /**
     * Convert the local file into an xml document
     * @return the xml document object for the file
     */
    protected Document formDocument(String filePath) {
        Document xmlDoc = null;
        
        // Return false if the file path is null or blank
        if(filePath == null || filePath.equals("")) {
            return null;
        }
        
        DocumentBuilder docBuilder;
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        File sourceFile = null;
        
        docBuilderFactory.setIgnoringElementContentWhitespace(true);
        
        try {
            docBuilder = docBuilderFactory.newDocumentBuilder();
        } catch (ParserConfigurationException parserEx) {
            System.out.println("Exception : " + parserEx.getMessage());
            parserEx.printStackTrace();
            return null;
        }
        
        try {
            sourceFile = new File(filePath);
        } catch(Exception e) {
            System.out.println("Invalid file:  " + path + ". Exception : " + e.getMessage());
            return null;
        }
        
        try {
            xmlDoc = docBuilder.parse(sourceFile);
        } catch (SAXException saxEx) {
            System.out.println("Wrong XML file structure. File :  " + path + ". Exception : " + saxEx.getMessage());
            saxEx.printStackTrace();
            return null;
        }  catch (IOException ioEx) {
            System.out.println("Could not read source file : " + path + ". Exception : " + ioEx.getMessage());
            //ioEx.printStackTrace();
            return null;
        }
        
        return xmlDoc;
    }
}
